require("../util.js");
require("../component/components.js");
require("../component/selfmediaflow.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
console.log(userInfo)
var vm = new Vue({
    el:"#userHomepage",
    data:{
        loadFlag:false,
        indexs:1,
        isNav:1,
        uid:userInfo.id,//当前用户id
        id:"", //用户id
        userInfo:{},//用户信息
        mediasList:{},
        mediasList1:[],
        mediasList2:[],
        productsList:{},
        productsList1:[],
        productsList2:[],
        pageNumber:1
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.id=this.searchObject&&this.searchObject.id;
        this.getUserInfo();
    },
    methods:{
        //后退
        getBack:function(){
            console.log("dddddd")
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //tab切换
        getNavtab:function(num){
            this.indexs=num;
            this.pageNumber=1;
            if(num==1){
                this.$nextTick(function(){
                    var swiper1 = new Swiper(".homepage-album .swiper-container",{
                        spaceBetween: 5,
                        slidesPerView: 'auto',
                        freeMode: true,
                        lazy: {
                            loadPrevNext: true,
                            loadPrevNextAmount: 3,
                            loadOnTransitionStart: true
                        },
                        observer: true,//修改swiper自己或子元素时，自动初始化swiper
                        observeParents: true//修改swiper的父元素时，自动初始化swiper
                    });
                });
            }else if(num==2){
                this.getUserMediasList();
            }else if(num==3){
                this.getUserProductsList();
            }
        },
        //自媒体用户信息
        getUserInfo:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                id:that.id
            };
            console.log(data)
            this.ajax({url:"/user/getUserInfo",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.userInfo=b.json.data;
                    that.getUserMediasList();
                    that.getUserProductsList();
                }else{
                    vm.$refs.prompt.toast("获取用户信息失败！")
                }
            });
        },
        //通过用户id 返回用户的自媒体列表
        getUserMediasList:function(){
            var that=this;
            var data={
                uid:that.userInfo.uid,
                pageNumber :that.pageNumber
            };
            console.log(data)
            this.ajax({url:"/user/getUserMediasList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.mediasList=b.json.data;
                    that.mediasList1=that.mediasList.list;
                    if(that.mediasList.pageNumber>1){
                        if(that.mediasList.list.length>0) {
                            that.mediasList2 = that.mediasList2.concat(b.json.data.list);
                        }
                    }else{
                        that.mediasList2= that.mediasList1;
                    }
                    vm.loadFlag=false;
                }else{
                    that.mediasList={};
                    that.mediasList1=[];
                    that.mediasList2=[];
                    vm.$refs.prompt.toast("获取视频列表失败！")
                }
            });
        },
        //通过用户id 返回用户的商品列表
        getUserProductsList:function(){
            var that=this;
            var data={
                uid:that.userInfo.uid,
                pageNumber :that.pageNumber
            };
            console.log(data)
            this.ajax({url:"/user/getUserProductsList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.productsList=b.json.data;
                    that.productsList1=that.productsList.list;
                    if(that.productsList.pageNumber>1){
                        if(that.productsList.list.length>0) {
                            that.productsList2 = that.productsList2.concat(b.json.data.list);
                        }
                    }else{
                        that.productsList2= that.productsList1;
                    }
                    vm.loadFlag=false;
                }else{
                    that.productsList={};
                    that.productsList1=[];
                    that.productsList2=[];
                    vm.$refs.prompt.toast("获取商品列表失败！")
                }
            });
        },
        //乐友-增加好友
        getAddFriend:function(item){
            var that=this;
            var data={
                loginUid:that.uid,
                friendId:that.userInfo.uid
            };
            console.log(data)
            this.ajax({url:"/friend/getAddFriend",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getUserInfo();
                }else{
                    vm.$refs.prompt.toast("增加好友失败！");
                }
            });
        },
        getNext:function(type,item){
            if(type==1){
                location.href="userHomepage?id="+item.uid
            }else if(type==2){
                if(this.userInfo.uid==userInfo.chatId){
                    vm.$refs.prompt.toast("不能和自己聊天！");
                }else{
                    location.href="/friend?type=1&circleId="+this.userInfo.uid+"&uid="+this.uid;
                }

            }else if(type==3){
                location.href="/friend/circleBar?type=3&circleId="+item.circleId+"&uid="+this.uid;
            }else if(type==4){
                location.href="/see/selfMediaVd?id="+item.mediaId+"&uid="+this.uid+"&sourceType=1";
            }else if(type==5){
                location.href="/happyi/myFans?type=5";
            }else if(type==6){
                location.href="/userGoodsInfo?productId="+item.productId;
            }
        }
    }
    ,watch:{
        /*mediasList2: function () {
            if(this.mediasList2.length!=0){
                this.$nextTick(function(){
                    fmasonry();
                    $("img.lazy").lazyload({
                        failurelimit:10,
                        load:fmasonry
                    });
                    console.log($(".selfMedia-tabCent").height());
                    // if(getWindowHeight())

                });
            }
        }*/
    }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    if(getDocumentTop()>135){
        vm.isNav=2;
    }else{
        vm.isNav=1;
    }
    if(vm.indexs!=1){
        var loadFlag;
        if(vm.indexs==2){
            loadFlag = vm.mediasList.lastPage;
        }else if(vm.indexs==3){
            loadFlag = vm.productsList.lastPage;
        }
        //监听事件内容
        if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
            //当滚动条到底时,这里是触发内容
            if(!loadFlag){
                vm.loadFlag=true;
                vm.pageNumber++;
                if(vm.indexs==2){
                    vm.getUserMediasList();
                }else if(vm.indexs==3){
                    vm.getUserProductsList();
                }
            }else{
                vm.$refs.prompt.toast("没有更多了！")
            }
        }
    }else{
        return false;
    }
};
// function fmasonry() {
//     minigrid('.selfMedia-tabCent', '.selfMedia-list',9);
// }
// function lazyload(){
//     $("img.lazy").lazyload();
// }